import React, { useState, useEffect } from 'react'
import { Col, Form, Input, Button, Tabs, Row, Select, Table, Avatar, Modal, DatePicker, Switch } from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import RenderButtom from '@/components/RenderButton'
import SearchBox from '@/components/SearchBox'
import { router } from 'umi'
import { getSupplierList, getStoreList, getBrankList } from '@/services/goodCenter/goodsManage/goodsBrand'
import storeList from '@/pages/supplier/store/storeList'

const { Option } = Select

// 表头
const columns = [
  {
    title: '品牌中文名称',
    dataIndex: 'nameCn',
    key: 'nameCn',
    align: 'center'
  },
  {
    title: '品牌英文名称',
    dataIndex: 'nameEn',
    key: 'nameEn',
    align: 'center'
  },
  {
    title: '品牌图片',
    dataIndex: 'logo',
    key: 'logo',
    align: 'center',
    render: logo => <Avatar shape="square" size="large" src={logo} />
  },
  {
    title: '排序',
    dataIndex: 'sort',
    key: 'sort',
    align: 'center'
  },
  {
    title: '加入推荐',
    dataIndex: 'joinIntroduce',
    key: 'joinIntroduce',
    align: 'center',
    render: r => <Switch defaultChecked={r == '1' ? true : false} />
  },
  {
    title: '是否显示',
    dataIndex: 'isShow',
    key: 'isShow',
    align: 'center',
    render: r => <Switch defaultChecked={r == '1' ? true : false} />
  },
  {
    title: '扣点率',
    dataIndex: 'deductionRate',
    key: 'deductionRate',
    align: 'center'
  }
]

const GoodsBrand = props => {
  const { form } = props
  const { getFieldDecorator } = form
  const [total, setTotal] = useState(1)
  const [pageNo, setPageNo] = useState(1)
  const [pageSize, setPageSize] = useState(10)
  const [record, setRecord] = useState({})
  const [supplierList, setSupplierList] = useState([])
  const [storeList, setStoreList] = useState([])
  const [brankList, setBrankList] = useState([])

  const rowSelection = {
    type: 'radio',
    onChange: (selectedRowKeys, selectedRows) => {
      setRecord(selectedRows)
      // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
      console.log(record)
    },
  }

  useEffect(() => {
    init()
  }, [])
  const init = async () => {
    await getSupplierLists()
    let params = {
      pageNo,
      pageSize
    }
    await getBrankLists(params)
  }
  // 表单查询
  const handleSearch = e => {
    e.preventDefault()
    form.validateFields(async (err, values) => {
      console.log('Received values of form: ', values)
      let params = {
        nameCn: values.nameCn,
        nameEn: values.nameEn,
        pageNo,
        pageSize,
        storeId: values.storeId,
        supplierId: values.supplierId
      }
      await getBrankLists(params)
    })
  }
  // 表单清空
  const handleReset = e => {
    form.resetFields();
  }
  // 查询条件
  const getFields = () => {
    return(
      <Row gutter={24}>
        <Col span={8}>
          <Form.Item label="品牌中文名称" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('nameCn', {})
              (<Input placeholder="请输入品牌中文名称" />)}
          </Form.Item>
        </Col>
        <Col span={8}>
          <Form.Item label="品牌英文名称" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('nameEn', {})
              (<Input placeholder="请输入品牌英文名称" />)}
          </Form.Item>
        </Col>
        <Col span={8}>
          <Form.Item label="所属商户" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('supplierId', {})
              (<Select
                style={{ width: '100%' }}
                showSearch
                filterOption={(input, option) => option.props.children.indexOf(input) >= 0}
                placeholder="请选择"
                onChange={selectSupplier}
              >
                { supplierList && supplierList.map(r => <Option key={r.id} value={r.id}>{r.merchantName}</Option>)}
              </Select>)}
          </Form.Item>
        </Col>
        <Col span={8}>
          <Form.Item label="门店" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} >
            {getFieldDecorator('storeId', {})
              (<Select
                style={{ width: '100%' }}
                showSearch
                filterOption={(input, option) => option.props.children.indexOf(input) >= 0}
                placeholder="请选择"
              >
                { storeList && storeList.map(r => <Option key={r.id} value={r.id}>{r.name}</Option>)}
              </Select>)}
          </Form.Item>
        </Col>
      </Row>
    )
  }
  // 按钮处理
  const handleBtn = async type => {
    console.log(type)
    switch (type) {
      // 添加品牌
      case 1:
        router.push({ pathname: '/goodsCenter/goodsManage/addGoodsBrand' })
        break
      // 编辑品牌
      case 3:
        if (JSON.stringify(record) == "{}") return
        router.push({ pathname: '/goodsCenter/goodsManage/addGoodsBrand', query: { brandId: record[0].id } })
        break
    }
  }

  const pagination = {
    total: parseInt(total),
    onChange(page) {
      let params = {
        pageNo: page,
        pageSize
      }
      getBrankLists(params)
    }
  }

  // 获取商户列表
  const getSupplierLists = async () => {
    let res = await getSupplierList()
    if (res.code === '0') {
      setSupplierList(res.data)
    } else {
      alert(res.msg)
    }
  }
  // 获取门店列表
  const getStoreLists = async (params) => {
    let res = await getStoreList(params)
    if (res.code === '0') {
      setStoreList(res.data)
    } else {
      alert(res.msg)
    }
  }
  // 选择商户
  const selectSupplier = async (supplierId) => {
    let params = {
      pageSize: 999,
      supplierId
    }
    await getStoreLists(params)
  }
  // 获取品牌列表
  const getBrankLists = async (params) => {
    let res = await getBrankList(params)
    if (res.code === '0') {
      setBrankList(res.data)
      setTotal(res.iTotalDisplayRecords)
    } else {
      alert(res.msg)
    }
  }

  return (
    <PageHeaderWrapper>
      <SearchBox handleReset={handleReset} handleSearch={handleSearch} getFields={getFields} />
      <RenderButtom click={handleBtn} />
      <Table
        columns={columns}
        dataSource={brankList}
        rowKey={record => record.id}
        pagination={pagination}
        rowSelection={rowSelection}
      />
   </PageHeaderWrapper>
  )
}

export default Form.create()(GoodsBrand)
